// 导入封装好的登陆接口
import { sysLoginRequest, sysProfileRequest } from '@/api/user.js'
// 导入存储token的cookies
import { getToken, setToken } from '@/utils/auth.js'
// 导入路由
import router, { constantRoutes } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {},
    routes: []
  },
  mutations: {
    steTokenMutations(state, payload) {
      state.token = payload
      setToken(payload)
    },
    setUserInforMutation(state, payload) {
      state.userInfo = payload
    },
    setRoutes(state, payload) {
      state.routes = [...constantRoutes, ...payload]
    }
  },
  getters: {},
  actions: {
    async loginActions({ commit }, payload) {
      try {
        const res = await sysLoginRequest(payload)
        console.log('在actions里面发请求测试:', res)
        commit('steTokenMutations', res.data)
        console.log(res.data)
        router.push('/')
      } catch (err) {
        // 收集一些错误日志
        console.log('收集错误：', err)
      }
    },
    async getUserInforAction({ commit }, payload) {
      try {
        const res = await sysProfileRequest()
        commit('setUserInforMutation', res.data)
        return Promise.resolve(res.data)
      } catch (err) {
        console.log('获同用户个人信息失败')
      }
    },
    lgnoutAction({ commit }) {
      commit('steTokenMutations', '')
      commit('setUserInforMutation', {})
    }
  }
}
